const express = require("express");
const Router = express.Router();
const db = require("../../db/sql/dbconfig");
const bodyParser = require("body-parser");
const { userList } = require("../../db/sql/db");
const { json } = require("../../db/json");
const { verifyPhone } = require("../../utils/verify")
const { encryptPwd } = require("../../utils/format")
Router.use(bodyParser.json());
Router.use(
  bodyParser.urlencoded({
    extended: false,
  })
);
Router.use(bodyParser.json());

// 获取用户信息
Router.get("/Details", (req, res) => {
  let { user_id } = req.query;
  if (!user_id) {
    let result = "drop";
    json(res, result);
    return;
  }
  db(userList.queryAll, [user_id], (result) => {
    res
      .json({
        code: 0,
        msg: "获取成功",
        result: result[0],
      })
      .status(200);
  });
});
// 修改头像
Router.post("/update/Avatar", (req, res) => {
  let { user_id, newAvatar } = req.body;
  if (!user_id || !newAvatar) {
    let result = "drop";
    json(res, result);
    return;
  }
  db(userList.update("avatar"), [newAvatar, user_id], (result) => {
    let type = "update";
    json(res, type);
  });
});

// 修改性别
Router.post("/update/Gender", (req, res) => {
  let { user_id, newGender } = req.body;
  if (!user_id || !newGender) {
    let result = "drop";
    json(res, result);
    return;
  }
  if (newGender < 0 || newGender >= 3) {
    res.json({
      code: 500,
      msg: "请输入合法的性别",
    });
    return;
  }
  db(userList.update("gender"), [newGender, user_id], (result) => {
    let type = "update";
    json(res, type);
  });
});

// 修改昵称
Router.post("/update/NickName", (req, res) => {
  let { user_id, newNickName } = req.body;
  if (!user_id || !newNickName) {
    let result = "drop";
    json(res, result);
    return;
  }
  db(userList.update("nick_name"), [newNickName, user_id], (result) => {
    let type = "update";
    json(res, type);
  });
});

// 修改年龄
Router.post("/update/Age", (req, res) => {
  let { user_id, newAge } = req.body;
  console.log(user_id, newAge);
  if (!user_id || !newAge) {
    let result = "drop";
    json(res, result);
    return;
  }
  if (newAge < 0 && newAge > 100) {
    res.json({
      code: 0,
      msg: "请输入合法的年龄",
    });
    return;
  }
  db(userList.update("age"), [newAge, user_id], (result) => {
    console.log(result);
    let type = "update";
    json(res, type);
  });
});

// 修改手机号码
Router.post("/update/Phone", (req, res) => {
  let { newPhone, user_id } = req.body;
  if (!newPhone || !user_id) {
    let result = "drop";
    json(res, result);
    return;
  }
  let result
  let isFlag = verifyPhone(newPhone, (msg) => {
    result = msg
  })
  if (!isFlag) {
    res.send(result)
    return
  }


  db(userList.update("phone"), [newPhone, user_id], (result) => {
    let type = "update";
    json(res, type);
  });
});

// 修改密码
Router.post("/update/amendPwd", (req, res) => {
  let { user_id, newPwd, oldPwd } = req.body
  if (!user_id || !newPwd || !oldPwd) {
    let result = "drop";
    json(res, result);
    return;
  }
  oldPwd = encryptPwd(oldPwd)
  newPwd = encryptPwd(newPwd)
  // 查询密码
  db(userList.queryPwd, [oldPwd, user_id], (result) => {
    console.log(result)
    if (!result.length) {
      res.send({
        code: 400,
        msg: "你输入的旧密码有误,请确认后再输入"
      })
      return
    }
    db(userList.update("password"), [newPwd, user_id], (data) => {
      let type = "update";
      json(res, type);

    })
  })

})

// 忘记密码
Router.post("/update/forgetPwd", (req, res) => {
  let { user_id, newPwd } = req.body
  newPwd = newPwd.trim()
  if (!user_id || !newPwd) {
    let result = "drop";
    json(res, result);
    return;
  }
  newPwd = encryptPwd(newPwd)
  db(userList.update("password"), [newPwd, user_id], (data) => {
    let type = "update";
    json(res, type);
  })
})

// 修改角色权限
Router.post("/update/role", (req, res) => {
  let { user_id, role } = req.body
  if (!user_id || !role) {
    let result = "drop";
    json(res, result);
    return;
  }
  db(userList.update("role"), [newPwd, user_id], (data) => {
    let type = "update";
    json(res, type);
  })
})

// 修改用户信息
Router.post("/update/userinfo",(req,res)=>{
  let {user_id,nick_name,role,gender,age,avatar,user_name,phone} = req.body
  console.log(req.body)
  // 验证手机号码
  let result
  let isFlag = verifyPhone(phone, (msg) => {
    result = msg
  })
  if (!isFlag) {
    res.send(result)
    return
  }
  // 验证年龄
  if(age<=0||age>=100){
    res.send({
      msg:"你输入的年龄有误",
      code:500,
    })
    return
  }
  db(userList.update("updateAll"),[nick_name,role,gender,age,avatar,user_name,phone,user_id],(data)=>{
    res.send({
      code:0,
      msg:"修改成功",
    })
  })
})






module.exports = Router;
